<!doctype html>



  


<html class="theme-next muse use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  
    
      
    

    
  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Microsoft YaHei:300,300italic,400,400italic,700,700italic|consolas:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="js," />








  <link rel="shortcut icon" type="image/x-icon" href="http://ofy7k3v4s.bkt.clouddn.com/mylogo.png?v=5.0.2" />






<meta name="description" content="String &amp;#x7C7B;&amp;#x578B;String &amp;#x7C7B;&amp;#x578B;&amp;#x662F;&amp;#x5B57;&amp;#x7B26;&amp;#x4E32;&amp;#x7684;&amp;#x5BF9;&amp;#x8C61;&amp;#x5305;&amp;#x88C5;&amp;#x7C7B;&amp;#x578B;&amp;#x3002;&amp;#x521B;&amp;#x5EFA;&amp;#x65B9;&amp;#x5F0F;&amp;#x6709;&amp;#x4E24;&amp;#x79CD;&amp;#">
<meta property="og:type" content="article">
<meta property="og:title" content="js String对象">
<meta property="og:url" content="https://hongqinma.github.io/2014/05/15/09-js-String对象/index.html">
<meta property="og:site_name" content="HongqinMa' Blog">
<meta property="og:description" content="String &amp;#x7C7B;&amp;#x578B;String &amp;#x7C7B;&amp;#x578B;&amp;#x662F;&amp;#x5B57;&amp;#x7B26;&amp;#x4E32;&amp;#x7684;&amp;#x5BF9;&amp;#x8C61;&amp;#x5305;&amp;#x88C5;&amp;#x7C7B;&amp;#x578B;&amp;#x3002;&amp;#x521B;&amp;#x5EFA;&amp;#x65B9;&amp;#x5F0F;&amp;#x6709;&amp;#x4E24;&amp;#x79CD;&amp;#">
<meta property="og:updated_time" content="2016-11-12T06:40:11.173Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="js String对象">
<meta name="twitter:description" content="String &amp;#x7C7B;&amp;#x578B;String &amp;#x7C7B;&amp;#x578B;&amp;#x662F;&amp;#x5B57;&amp;#x7B26;&amp;#x4E32;&amp;#x7684;&amp;#x5BF9;&amp;#x8C61;&amp;#x5305;&amp;#x88C5;&amp;#x7C7B;&amp;#x578B;&amp;#x3002;&amp;#x521B;&amp;#x5EFA;&amp;#x65B9;&amp;#x5F0F;&amp;#x6709;&amp;#x4E24;&amp;#x79CD;&amp;#">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '6347850919329138000',
      author: 'Hiraku'
    }
  };
</script>




  <link rel="canonical" href="https://hongqinma.github.io/2014/05/15/09-js-String对象/"/>


  <title> js String对象 | HongqinMa' Blog </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="en">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?10010323";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta custom-logo">
  
    <div class="site-meta-headline">
      <a>
        <img class="custom-logo-image" src="http://ofy7k3v4s.bkt.clouddn.com/photo.jpg"
             alt="HongqinMa' Blog"/>
      </a>
    </div>
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">HongqinMa' Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">Spell at all costs， to rush you in the future。</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            About
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            Tags
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                js String对象
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">Posted on</span>
            <time itemprop="dateCreated" datetime="2014-05-15T22:19:03+08:00" content="2014-05-15">
              2014-05-15
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">In</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/JavaScript/" itemprop="url" rel="index">
                    <span itemprop="name">JavaScript</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2014/05/15/09-js-String对象/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2014/05/15/09-js-String对象/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          

          
              &nbsp; | &nbsp;
              <span class="page-pv"><i class="fa fa-file-o"></i>
              <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
              </span>
          

          
            <span id="busuanzi_container_page_pv">  <span id="busuanzi_value_page_pv"></span> Views</span>
          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="String-&#x7C7B;&#x578B;"><a href="#String-&#x7C7B;&#x578B;" class="headerlink" title="String &#x7C7B;&#x578B;"></a>String &#x7C7B;&#x578B;</h2><p>String &#x7C7B;&#x578B;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x5BF9;&#x8C61;&#x5305;&#x88C5;&#x7C7B;&#x578B;&#x3002;&#x521B;&#x5EFA;&#x65B9;&#x5F0F;&#x6709;&#x4E24;&#x79CD;&#xFF1A;</p>
<ul>
<li>var strObject = new String(&#x201C;abc&#x201D;);</li>
<li>var strValue = &#x201C;abc&#x201D;;</li>
</ul>
<p>&#x5176;&#x4E2D;&#xFF0C;&#x7EE7;&#x627F;&#x7684; valueOf()&#xFF0C;toLocaleString()&#xFF0C;toString() &#x65B9;&#x6CD5;&#xFF0C;&#x90FD;&#x8FD4;&#x56DE;&#x5BF9;&#x8C61;&#x6240;&#x8868;&#x793A;&#x7684;&#x57FA;&#x672C;&#x5B57;&#x7B26;&#x4E32;&#x503C;&#x3002;</p>
<p>String &#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#x90FD;&#x6709;&#x4E00;&#x4E2A; length &#x5C5E;&#x6027;&#xFF0C;&#x8868;&#x793A;&#x5305;&#x542B;&#x591A;&#x5C11;&#x4E2A;&#x5B57;&#x7B26;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x83B7;&#x53D6;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x957F;&#x5EA6;&#x3002;</p>
<hr>
<h2 id="&#x5B57;&#x7B26;&#x65B9;&#x6CD5;"><a href="#&#x5B57;&#x7B26;&#x65B9;&#x6CD5;" class="headerlink" title="&#x5B57;&#x7B26;&#x65B9;&#x6CD5;:"></a>&#x5B57;&#x7B26;&#x65B9;&#x6CD5;:</h2><h3 id="charAt-&#x548C;-charCodeAt"><a href="#charAt-&#x548C;-charCodeAt" class="headerlink" title="charAt() &#x548C; charCodeAt()"></a>charAt() &#x548C; charCodeAt()</h3><ul>
<li>charAt(&#x4E0B;&#x6807;);&#x83B7;&#x53D6;&#x8BE5;&#x4E0B;&#x6807;&#x4F4D;&#x7F6E;&#x7684;&#x5B57;&#x7B26;&#x4E32;</li>
<li>charCodeAt(&#x4E0B;&#x6807;);&#x83B7;&#x53D6;&#x5BF9;&#x5E94;&#x4E0B;&#x6807;&#x5B57;&#x7B26;&#x4E32;&#x7684;ASCII&#x7684;&#x7801;&#x503C;<ul>
<li>charAt() &#x548C; charCodeAt() &#x65B9;&#x6CD5;&#x63A5;&#x6536;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x5373;&#x57FA;&#x4E8E; 0 &#x7684;&#x5B57;&#x7B26;&#x4F4D;&#x7F6E;</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">&quot;abc&quot;</span>;</div><div class="line"><span class="built_in">console</span>.log(str.charAt(<span class="number">1</span>));  <span class="comment">// &quot;a&quot;</span></div></pre></td></tr></table></figure>
<ul>
<li>concat(&#x5B57;&#x7B26;&#x4E32;);<ul>
<li>&#x62FC;&#x63A5;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x7528;&#x4E8E;&#x5C06;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x62FC;&#x63A5;&#x8D77;&#x6765;&#xFF0C;&#x5F97;&#x5230;&#x65B0;&#x4E32;</li>
<li>concat &#x53EF;&#x4EE5;&#x63A5;&#x6536;&#x4EFB;&#x610F;&#x591A;&#x4E2A;&#x53C2;&#x6570;</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">&quot;abc&quot;</span>;</div><div class="line"><span class="built_in">console</span>.log(str.concat(<span class="string">&quot;de&quot;</span>, <span class="string">&quot;fgh&quot;</span>)); <span class="comment">// &quot;abcdefgh&quot;</span></div></pre></td></tr></table></figure>
<h3 id="slice-&#x3001;substring-&#x548C;-substr"><a href="#slice-&#x3001;substring-&#x548C;-substr" class="headerlink" title="slice()&#x3001;substring() &#x548C; substr()"></a>slice()&#x3001;substring() &#x548C; substr()</h3><ul>
<li>slice(&#x5F00;&#x59CB;&#x4E0B;&#x6807;,&#x7ED3;&#x675F;&#x4E0B;&#x6807;);&#x622A;&#x53D6;&#x4E00;&#x6BB5;&#x5B57;&#x7B26;&#x4E32;&#x7684;</li>
<li>substring(&#x5F00;&#x59CB;&#x7684;&#x4E0B;&#x6807;,&#x7ED3;&#x675F;&#x7684;&#x4E0B;&#x6807;);&#x622A;&#x53D6;&#x6307;&#x5B9A;&#x90E8;&#x5206;&#x7684;&#x5B57;&#x7B26;&#x4E32;</li>
<li>substr(&#x5F00;&#x59CB;&#x7684;&#x4E0B;&#x6807;&#xFF0C;&#x622A;&#x53D6;&#x7684;&#x4E2A;&#x6570;);&#x622A;&#x53D6;&#x6307;&#x5B9A;&#x957F;&#x5EA6;&#x7684;&#x5B57;&#x7B26;&#x4E32;<ul>
<li>slice(), substring(), substr() &#x8FD9;&#x4E09;&#x4E2A;&#x65B9;&#x6CD5;&#x90FD;&#x4F1A;&#x8FD4;&#x56DE;&#x88AB;&#x64CD;&#x4F5C;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x4E00;&#x4E2A;&#x5B50;&#x4E32;</li>
<li>&#x90FD;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x6216;&#x4E24;&#x4E2A;&#x53C2;&#x6570;</li>
<li>&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x8868;&#x793A;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#x5230;&#x54EA;&#x91CC;&#x7ED3;&#x675F;</li>
<li>slice() &#x548C; substring() &#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x540E;&#x9762;&#x7684;&#x4F4D;&#x7F6E;</li>
<li>substr() &#x7684;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x6307;&#x8FD4;&#x56DE;&#x7684;&#x5B57;&#x7B26;&#x4E2A;&#x6570;</li>
<li>&#x5982;&#x679C;&#x6CA1;&#x6709;&#x7ED9;&#x8FD9;&#x4E09;&#x4E2A;&#x65B9;&#x6CD5;&#x6307;&#x5B9A;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x5219;&#x5C06;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x957F;&#x5EA6;&#x4F5C;&#x4E3A;&#x7ED3;&#x675F;&#x4F4D;&#x7F6E;&#x3002;</li>
<li>&#x8FD9;&#x51E0;&#x4E2A;&#x65B9;&#x6CD5;&#x90FD;&#x8FD4;&#x56DE;&#x65B0;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x4E0D;&#x5F71;&#x54CD;&#x539F;&#x5B57;&#x7B26;&#x4E32;.</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">&quot;Hello World&quot;</span>;</div><div class="line"><span class="built_in">console</span>.log(str.slice(<span class="number">3</span>));  <span class="comment">// &quot;Hello World&quot;</span></div><div class="line"><span class="built_in">console</span>.log(str.substring(<span class="number">3</span>)); <span class="comment">// &quot;Hello World&quot;</span></div><div class="line"><span class="built_in">console</span>.log(str.substr(<span class="number">3</span>)); <span class="comment">// &quot;Hello World&quot;</span></div><div class="line"><span class="built_in">console</span>.log(str.slice(<span class="number">3</span>, <span class="number">7</span>)); <span class="comment">// &quot;lo W&quot;</span></div><div class="line"><span class="built_in">console</span>.log(str.subtring(<span class="number">3</span>, <span class="number">7</span>)); <span class="comment">// &quot;lo W&quot;</span></div><div class="line"><span class="built_in">console</span>.log(str.subtr(<span class="number">3</span>, <span class="number">7</span>)); <span class="comment">// &quot;lo Worl&quot;</span></div></pre></td></tr></table></figure>
<ul>
<li>&#x4F20;&#x5165;&#x8D1F;&#x503C;&#x65F6;&#xFF0C;slice() &#x65B9;&#x6CD5;&#x4F1A;&#x5C06;&#x4F20;&#x5165;&#x7684;&#x8D1F;&#x503C;&#x4E0E;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x957F;&#x5EA6;&#x76F8;&#x52A0;</li>
<li>substr() &#x65B9;&#x6CD5;&#x5C06;&#x8D1F;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x52A0;&#x8F7D;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x957F;&#x5EA6;&#xFF0C;&#x800C;&#x5C06;&#x8D1F;&#x7684;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x8F6C;&#x4E3A; 0 </li>
<li>substring() &#x65B9;&#x6CD5;&#x4F1A;&#x628A;&#x6240;&#x6709;&#x8D1F;&#x503C;&#x53C2;&#x6570;&#x90FD;&#x8F6C;&#x6362;&#x4E3A; 0 &#x3002;</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">&quot;Hello World&quot;</span>;</div><div class="line"><span class="built_in">console</span>.log(str.slice(<span class="number">-3</span>));  <span class="comment">// &quot;rld&quot;</span></div><div class="line"><span class="built_in">console</span>.log(str.substring(<span class="number">-3</span>)); <span class="comment">// &quot;Hello World&quot;</span></div><div class="line"><span class="built_in">console</span>.log(str.substr(<span class="number">-3</span>)); <span class="comment">// &quot;rld&quot;</span></div><div class="line"><span class="built_in">console</span>.log(str.slice(<span class="number">3</span>, <span class="number">-4</span>)); <span class="comment">// &quot;lo W&quot; &#x4F1A;&#x5C06;&#x4F20;&#x5165;&#x7684;&#x8D1F;&#x503C;&#x4E0E;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x957F;&#x5EA6;&#x76F8;&#x52A0;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x503C;&#x8F6C;&#x6210;&#x4E86;7</span></div><div class="line"><span class="built_in">console</span>.log(str.subtring(<span class="number">3</span>, <span class="number">-4</span>)); <span class="comment">// &quot;Hel&quot;, &#x4F1A;&#x5C06;&#x8F83;&#x5C0F;&#x7684;&#x6570;&#x4F5C;&#x4E3A;&#x8D77;&#x59CB;&#x4F4D;&#x7F6E;&#xFF0C;&#x5C06;&#x8F83;&#x5927;&#x7684;&#x8BF4;&#x4F5C;&#x4E3A;&#x7ED3;&#x675F;&#x4F4D;&#x7F6E;</span></div><div class="line"><span class="built_in">console</span>.log(str.subtr(<span class="number">3</span>, <span class="number">-4</span>)); <span class="comment">// &quot;&quot;</span></div></pre></td></tr></table></figure>
<h3 id="toLocaleUpperCase-&#x3001;toUpperCase-&#x3001;toLocaleLowerCase-&#x3001;toLowerCase"><a href="#toLocaleUpperCase-&#x3001;toUpperCase-&#x3001;toLocaleLowerCase-&#x3001;toLowerCase" class="headerlink" title="toLocaleUpperCase()&#x3001;toUpperCase()&#x3001;toLocaleLowerCase()&#x3001;toLowerCase()"></a>toLocaleUpperCase()&#x3001;toUpperCase()&#x3001;toLocaleLowerCase()&#x3001;toLowerCase()</h3><ul>
<li>toLocaleUpperCase  &#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x5927;&#x5199;, &#x9488;&#x5BF9;&#x7279;&#x5B9A;&#x5730;&#x533A;</li>
<li>toUpperCase  &#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x5927;&#x5199;</li>
<li>toLocaleLowerCase  &#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x5C0F;&#x5199;, &#x9488;&#x5BF9;&#x7279;&#x5B9A;&#x5730;&#x533A;</li>
<li>toLowerCase  &#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x5C0F;&#x5199;</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">&quot;Hello World&quot;</span>;</div><div class="line"><span class="built_in">console</span>.log(str.toLocaleUpperCase());  <span class="comment">// &quot;HELLO WORLD&quot;</span></div><div class="line"><span class="built_in">console</span>.log(str.toUpperCase());  <span class="comment">// &quot;HELLO WORLD&quot;</span></div><div class="line"><span class="built_in">console</span>.log(str.toLocaleLowerCase());  <span class="comment">// &quot;hello world&quot;</span></div><div class="line"><span class="built_in">console</span>.log(str.toLowerCase());  <span class="comment">// &quot;hello world&quot;</span></div></pre></td></tr></table></figure>
<h3 id="indexOf-&#x548C;-lastIndexOf"><a href="#indexOf-&#x548C;-lastIndexOf" class="headerlink" title="indexOf() &#x548C; lastIndexOf()"></a>indexOf() &#x548C; lastIndexOf()</h3><ul>
<li>indexOf &#x67E5;&#x627E;&#x6307;&#x5B9A;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x627E;&#x5230;&#x5219;&#x8FD4;&#x56DE;&#x4E0B;&#x6807;&#xFF0C;&#x627E;&#x4E0D;&#x5230;&#x8FD4;&#x56DE;-1</li>
<li>lastIndexOf &#x4ECE;&#x540E;&#x9762;&#x5411;&#x524D;&#x9762;&#x627E;&#x6307;&#x5B9A;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x627E;&#x4E0D;&#x5230;&#x4E5F;&#x662F;&#x8FD4;&#x56DE;&#x7684;&#x662F;-1<ul>
<li>indexOf() &#x548C; lastIndexOf() &#x65B9;&#x6CD5;&#x90FD;&#x662F;&#x4ECE;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x641C;&#x7D22;&#x7ED9;&#x5B9A;&#x7684;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;</li>
<li>&#x8FD4;&#x56DE;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x4F4D;&#x7F6E;</li>
<li>&#x627E;&#x4E0D;&#x5230;&#x5219;&#x8FD4;&#x56DE; -1 </li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">&quot;Hello World&quot;</span>;</div><div class="line"><span class="built_in">console</span>.log(str.indexOf(<span class="string">&quot;o&quot;</span>));  <span class="comment">// 4</span></div><div class="line"><span class="built_in">console</span>.log(str.lastIndexOf(<span class="string">&quot;o&quot;</span>));  <span class="comment">// 7</span></div></pre></td></tr></table></figure>
<ul>
<li>indexOf() &#x548C; lastIndexOf() &#x65B9;&#x6CD5;&#x90FD;&#x53EF;&#x4EE5;&#x63A5;&#x6536;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x8868;&#x793A;&#x4ECE;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x54EA;&#x4E2A;&#x4F4D;&#x7F6E;&#x5F00;&#x59CB;&#x641C;&#x7D22;</li>
<li>indexOf() &#x4F1A;&#x4ECE;&#x6307;&#x5B9A;&#x4F4D;&#x7F6E;&#x5411;&#x540E;&#x641C;&#x7D22;&#xFF0C;&#x5FFD;&#x7565;&#x6307;&#x5B9A;&#x4F4D;&#x7F6E;&#x4E4B;&#x524D;&#x7684;</li>
<li>lastIndexOf() &#x4F1A;&#x4ECE;&#x6307;&#x5B9A;&#x4F4D;&#x7F6E;&#x5411;&#x524D;&#x641C;&#x7D22;&#xFF0C;&#x5FFD;&#x7565;&#x6307;&#x5B9A;&#x4F4D;&#x7F6E;&#x4E4B;&#x540E;&#x7684;</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">&quot;Hello World&quot;</span>;</div><div class="line"><span class="built_in">console</span>.log(str.indexOf(<span class="string">&quot;o&quot;</span>, <span class="number">6</span>));  <span class="comment">// 7</span></div><div class="line"><span class="built_in">console</span>.log(str.lastIndexOf(<span class="string">&quot;o&quot;</span>, <span class="number">6</span>));  <span class="comment">// 4</span></div></pre></td></tr></table></figure>
<p>&#x67E5;&#x627E;&#x67D0;&#x4E2A;&#x5143;&#x7D20;&#x5728;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x51FA;&#x73B0;&#x7684;&#x4F4D;&#x7F6E;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">&quot;Spell at all costs&#xFF0C; to rush you in the future&#x3002;&quot;</span>,</div><div class="line">    position = [],</div><div class="line">    pos = str.indexOf(<span class="string">&quot;t&quot;</span>);</div><div class="line"><span class="keyword">while</span>( pos &gt; <span class="number">-1</span> ){</div><div class="line">  position.push(pos);</div><div class="line">  pos = str.indexOf(<span class="string">&quot;t&quot;</span>, pos + <span class="number">1</span>); </div><div class="line">}</div><div class="line"><span class="built_in">console</span>.log(position); <span class="comment">// [7, 16, 20, 35, 41]</span></div></pre></td></tr></table></figure>
<ul>
<li>trim  &#x5207;&#x6389;&#x5B57;&#x7B26;&#x4E32;&#x524D;&#x9762;&#x548C;&#x540E;&#x9762;&#x7684;&#x7A7A;&#x683C;<ul>
<li>&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x526F;&#x672C;&#xFF0C;&#x5220;&#x9664;&#x524D;&#x7F6E;&#x4EE5;&#x53CA;&#x540E;&#x7F00;&#x7A7A;&#x683C;&#xFF0C;&#x7136;&#x540E;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">&quot;    Hello World   &quot;</span>;</div><div class="line"><span class="keyword">var</span> strTrim = str.trim();</div><div class="line"><span class="built_in">console</span>.log(str);  <span class="comment">// &quot;    Hello World   &quot;</span></div><div class="line"><span class="built_in">console</span>.log(strTrim);  <span class="comment">// &quot;Hello World&quot;</span></div></pre></td></tr></table></figure>
<h3 id="&#x5B57;&#x7B26;&#x4E32;&#x5339;&#x914D;&#x6A21;&#x5F0F;&#x65B9;&#x6CD5;"><a href="#&#x5B57;&#x7B26;&#x4E32;&#x5339;&#x914D;&#x6A21;&#x5F0F;&#x65B9;&#x6CD5;" class="headerlink" title="&#x5B57;&#x7B26;&#x4E32;&#x5339;&#x914D;&#x6A21;&#x5F0F;&#x65B9;&#x6CD5;"></a>&#x5B57;&#x7B26;&#x4E32;&#x5339;&#x914D;&#x6A21;&#x5F0F;&#x65B9;&#x6CD5;</h3><ul>
<li>match() &#x76F4;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x8981;&#x4E48;&#x662F;&#x4E00;&#x4E2A;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x8981;&#x4E48;&#x662F;&#x4E00;&#x4E2A; RegExp &#x5BF9;&#x8C61;</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">&quot;cat, bat, sat, fat&quot;</span>;</div><div class="line"><span class="keyword">var</span> pattern = <span class="regexp">/.at/</span>;</div><div class="line"><span class="comment">// &#x4E0E; pattern.exec() &#x65B9;&#x6CD5;&#x76F8;&#x540C;</span></div><div class="line"><span class="keyword">var</span> matches = str.match(pattern);</div><div class="line"><span class="built_in">console</span>.log(matches.index);  <span class="comment">// 0</span></div><div class="line"><span class="built_in">console</span>.log(matches[<span class="number">0</span>]);  <span class="comment">// &quot;cat&quot;</span></div><div class="line"><span class="built_in">console</span>.log(matches);  <span class="comment">// [&quot;cat&quot;, index: 0, input: &quot;cat, bat, sat, fat&quot;]</span></div><div class="line"><span class="built_in">console</span>.log(pattern.lastIndex);  <span class="comment">// 0</span></div></pre></td></tr></table></figure>
<p>&#x4EE5;&#x4E0A;&#x7684;&#x5B9E;&#x4F8B;&#x4E2D; match &#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E86;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#xFF1B; &#x5982;&#x679C;&#x662F;&#x8C03;&#x7528; RegExp &#x5BF9;&#x8C61;&#x7684; exec() &#x65B9;&#x6CD5;&#x5E76;,&#x4F20;&#x9012;&#x672C;&#x4F8B;&#x4E2D;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x90A3;&#x4E48;&#x4E5F;&#x4F1A;&#x5F97;&#x5230;&#x4E0E;&#x6B64;&#x76F8;&#x540C;&#x7684;&#x6570;&#x7EC4;&#xFF1A;&#x6570;&#x7EC4;&#x7684;&#x7B2C;&#x4E00;&#x9879;&#x662F;&#x4E0E;&#x6574;&#x4E2A;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x4E4B;&#x540E;&#x7684;&#x6BCF;&#x4E00;&#x9879;&#xFF08;&#x5982;&#x679C;&#x6709;&#xFF09;&#x4FDD;&#x5B58;&#x7740;&#x4E0E;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x7684;&#x6355;&#x83B7;&#x7EC4;&#x5339;&#x914D;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF1B;</p>
<ul>
<li>search() &#x65B9;&#x6CD5;<ul>
<li>&#x552F;&#x4E00;&#x53C2;&#x6570;&#x4E0E; match() &#x65B9;&#x6CD5;&#x7684;&#x76F8;&#x540C;&#xFF1A;</li>
<li>&#x7531;&#x5B57;&#x7B26;&#x4E32;&#x6216; RegExp &#x5BF9;&#x8C61;&#x6307;&#x5B9A;&#x7684;&#x4E00;&#x4E2A;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x3002;</li>
<li>&#x8BE5;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7B2C;&#x4E00;&#x4E2A;&#x5339;&#x914D;&#x9879;&#x7684;&#x7D22;&#x5F15;&#xFF1B;</li>
<li>&#x5982;&#x679C;&#x6CA1;&#x6709;&#x627E;&#x5230;&#x8FD4;&#x56DE; -1</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">&quot;cat, bat, sat, fat&quot;</span>;</div><div class="line"><span class="keyword">var</span> pos = str.search(<span class="regexp">/at/</span>);</div><div class="line"><span class="built_in">console</span>.log(pos);  <span class="comment">// 1</span></div></pre></td></tr></table></figure>
<ul>
<li>replace() &#x65B9;&#x6CD5;<ul>
<li>&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;<ul>
<li>&#x7B2C;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x662F;&#x4E00;&#x4E2A; RegExp &#x5BF9;&#x8C61;&#x6216;&#x8005;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF08;&#x8FD9;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x4E0D;&#x4F1A;&#x88AB;&#x8F6C;&#x6210;&#x6B63;&#x5219;&#x8868;&#x793A;&#xFF09;</li>
<li>&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x662F;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x6216;&#x8005;&#x4E00;&#x4E2A;&#x51FD;&#x6570;</li>
</ul>
</li>
<li>&#x5982;&#x679C;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x53EA;&#x4F1A;&#x66FF;&#x6362;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;</li>
<li>&#x8981;&#x66FF;&#x6362;&#x6240;&#x6709;&#xFF0C;&#x6307;&#x5B9A;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x6307;&#x5B9A; g &#x6807;&#x5FD7;</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">&quot;cat, bat, sat, fat&quot;</span>;</div><div class="line"><span class="keyword">var</span> res1 = str.replace(<span class="string">&quot;at&quot;</span>, <span class="string">&quot;ond&quot;</span>);</div><div class="line"><span class="keyword">var</span> res2 = str.replace(<span class="regexp">/at/g</span>, <span class="string">&quot;ond&quot;</span>);</div><div class="line"><span class="built_in">console</span>.log(res1);  <span class="comment">// &quot;cond, bat, sat, fat&quot;</span></div><div class="line"><span class="built_in">console</span>.log(res2);  <span class="comment">// &quot;cond, bond, sond, fond&quot;</span></div></pre></td></tr></table></figure>
<ul>
<li>replace() &#x65B9;&#x6CD5;&#x7684;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x662F;&#x51FD;&#x6570;&#x3002;<ul>
<li>&#x5728;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x5339;&#x914D;&#x9879;&#xFF08;&#x5373;&#x4E0E;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF09;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4F1A;&#x5411;&#x51FD;&#x6570;&#x4F20;&#x9012;&#x4E09;&#x4E2A;&#x53C2;&#x6570;</li>
<li>&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x9879;&#x3001;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x9879;&#x5728;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;&#x548C;&#x539F;&#x59CB;&#x5B57;&#x7B26;&#x4E32;&#x3002;</li>
<li>&#x5728;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x5B9A;&#x4E49;&#x4E86;&#x591A;&#x4E2A;&#x6355;&#x83B7;&#x7EC4;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;</li>
<li>&#x4F20;&#x9012;&#x7ED9;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x4E00;&#x6B21;&#x662F;&#x6A21;&#x5F0F;&#x7684;&#x5339;&#x914D;&#x9879;&#x3001;&#x7B2C;&#x4E00;&#x4E2A;&#x6355;&#x83B7;&#x7EC4;&#x7684;&#x5339;&#x914D;&#x9879;&#x3001;&#x7B2C;&#x4E8C;&#x4E2A;&#x6355;&#x83B7;&#x7EC4;&#x7684;&#x5339;&#x914D;&#x9879;&#x2026;&#x2026;</li>
<li>&#x6700;&#x540E;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x4F9D;&#x7136;&#x5206;&#x522B;&#x662F;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x9879;&#x5728;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;&#x548C;&#x539F;&#x59CB;&#x5B57;&#x7B26;&#x4E32;&#x3002;</li>
<li>&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x8868;&#x793A;&#x5E94;&#x8BE5;&#x88AB;&#x66FF;&#x6362;&#x7684;&#x5339;&#x914D;&#x9879;&#x3002;</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">htmlEscape</span>(<span class="params">text</span>)</span>{</div><div class="line">  <span class="keyword">return</span> text.replace(<span class="regexp">/[&lt;&gt;&quot;&amp;]/g</span>, <span class="function"><span class="keyword">function</span>(<span class="params">match, pos, orginText</span>)</span>{</div><div class="line">    <span class="keyword">switch</span>(match){</div><div class="line">      <span class="keyword">case</span> <span class="string">&quot;&lt;&quot;</span>:</div><div class="line">        <span class="keyword">return</span> <span class="string">&quot;&amp;lt;&quot;</span>;</div><div class="line">      <span class="keyword">case</span> <span class="string">&quot;&gt;&quot;</span>:</div><div class="line">        <span class="keyword">return</span> <span class="string">&quot;&amp;gt;&quot;</span>;</div><div class="line">      <span class="keyword">case</span> <span class="string">&quot;&amp;&quot;</span>:</div><div class="line">        <span class="keyword">return</span> <span class="string">&quot;&amp;amp;&quot;</span>;</div><div class="line">      <span class="keyword">case</span> <span class="string">&quot;\&quot;&quot;</span>:</div><div class="line">        <span class="keyword">return</span> <span class="string">&quot;&amp;quot;&quot;</span>;</div><div class="line">    }</div><div class="line">  });</div><div class="line">}</div><div class="line"><span class="built_in">console</span>.log(htmlEscape(<span class="string">&quot;&lt;p class=\&quot;greeting\&quot;&gt;Hello World!&lt;/p&gt;&quot;</span>));</div><div class="line"><span class="comment">// &amp;lt;p class=&amp;quot;greeting&amp;quot;&amp;gt;Hello World!&amp;lt;/p&amp;gt;</span></div></pre></td></tr></table></figure>
<ul>
<li>split &#x5207;&#x5272;&#x6307;&#x5B9A;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;<ul>
<li>&#x57FA;&#x4E8E;&#x6307;&#x5B9A;&#x7684;&#x5206;&#x9694;&#x7B26;&#x5C06;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x5206;&#x9694;&#x6210;&#x591A;&#x4E2A;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5E76;&#x5C06;&#x7ED3;&#x679C;&#x653E;&#x5728;&#x6570;&#x7EC4;&#x4E2D;</li>
<li>&#x5206;&#x9694;&#x7B26;&#x53EF;&#x4EE5;&#x662F;&#x4E00;&#x4E2A; RegExp &#x5BF9;&#x8C61;&#x3002;</li>
<li>&#x53EF;&#x63A5;&#x53D7;&#x7B2C;&#x4E8C;&#x4E2A;&#x53EF;&#x9009;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x6307;&#x5B9A;&#x6570;&#x7EC4;&#x4E2D;&#x5927;&#x5C0F;&#x3002;</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> color = <span class="string">&quot;red,green,yellow,blue&quot;</span>;</div><div class="line"><span class="keyword">var</span> colors1 = color.split(<span class="string">&quot;,&quot;</span>);</div><div class="line"><span class="built_in">console</span>.log(colors1);  <span class="comment">// [&quot;red&quot;, &quot;green&quot;, &quot;yellow&quot;, &quot;blue&quot;]</span></div><div class="line"><span class="keyword">var</span> colors2 = color.split(<span class="string">&quot;,&quot;</span>, <span class="number">2</span>);</div><div class="line"><span class="built_in">console</span>.log(colors2);  <span class="comment">// [&quot;red&quot;, &quot;green&quot;]</span></div><div class="line"><span class="keyword">var</span> colors3 = color.split(<span class="regexp">/[^\,]+/</span>);</div><div class="line"><span class="built_in">console</span>.log(colors3);  <span class="comment">//[&quot;&quot;, &quot;,&quot;, &quot;,&quot;, &quot;,&quot;, &quot;&quot;]</span></div></pre></td></tr></table></figure>
<h3 id="localCompare"><a href="#localCompare" class="headerlink" title="localCompare()"></a>localCompare()</h3><p>&#x6B64;&#x65B9;&#x6CD5;&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#xFF1A;</p>
<ul>
<li>&#x82E5;&#x5B57;&#x7B26;&#x4E32;&#x518D;&#x5B57;&#x6BCD;&#x8868;&#x4E2D;&#x6392;&#x5728;&#x5B57;&#x7B26;&#x4E32;&#x53C2;&#x6570;&#x4E4B;&#x524D;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x8D1F;&#x6570;&#xFF0C;&#x5927;&#x591A;&#x6570;&#x60C5;&#x51B5;&#x4E0B;&#x4E3A; -1</li>
<li>&#x82E5;&#x5B57;&#x7B26;&#x4E32;&#x9093;&#x6BC5;&#x5B57;&#x7B26;&#x4E32;&#x53C2;&#x6570;&#xFF0C;&#x8FD4;&#x56DE; 0 </li>
<li>&#x82E5;&#x5B57;&#x7B26;&#x4E32;&#x518D;&#x5B57;&#x6BCD;&#x8868;&#x4E2D;&#x6392;&#x5728;&#x5B57;&#x7B26;&#x4E32;&#x53C2;&#x6570;&#x4E4B;&#x540E;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6B63;&#x6570;&#xFF0C;&#x5927;&#x591A;&#x6570;&#x60C5;&#x51B5;&#x4E0B;&#x4E3A; 1</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">&quot;yellow&quot;</span>;</div><div class="line"><span class="built_in">console</span>.log(str.localCompare(<span class="string">&quot;brick&quot;</span>));  <span class="comment">// 1</span></div><div class="line"><span class="built_in">console</span>.log(str.localCompare(<span class="string">&quot;yellow&quot;</span>));  <span class="comment">// 0</span></div><div class="line"><span class="built_in">console</span>.log(str.localCompare(<span class="string">&quot;zoo&quot;</span>));  <span class="comment">// -1</span></div></pre></td></tr></table></figure>
<h3 id="fromCharCode"><a href="#fromCharCode" class="headerlink" title="fromCharCode()"></a>fromCharCode()</h3><p>&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x662F; String &#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#xFF0C;&#x63A5;&#x6536;&#x4E00;&#x4E2A;&#x591A;&#x5B57;&#x7B26;&#x7F16;&#x7801;&#xFF0C;&#x8F6C;&#x5316;&#x4E3A;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">console</span>.log(<span class="built_in">String</span>.fromCharCode(<span class="number">104</span>, <span class="number">101</span>, <span class="number">108</span>, <span class="number">108</span>, <span class="number">111</span>)); <span class="comment">// &quot;hello&quot;</span></div></pre></td></tr></table></figure>
<h3 id="HTML-&#x65B9;&#x6CD5;"><a href="#HTML-&#x65B9;&#x6CD5;" class="headerlink" title="HTML &#x65B9;&#x6CD5;"></a>HTML &#x65B9;&#x6CD5;</h3><ul>
<li>anchor()  <code>&lt;a name=&quot;name&quot;&gt;string&lt;/a&gt;</code></li>
<li>big() <code>&lt;big&gt;string&lt;/big&gt;</code></li>
<li>bold() <code>&lt;b&gt;string&lt;/b&gt;</code></li>
<li>fixd() <code>&lt;tt&gt;string&lt;/tt&gt;</code></li>
<li>fontcolor(color) <code>&lt;font color=&quot;color&quot;&gt;string&lt;/font&gt;</code></li>
<li>fontsize(size) <code>&lt;font size=&quot;size&quot;&gt;string&lt;/font&gt;</code></li>
<li>italics() <code>&lt;i&gt;string&lt;/i&gt;</code></li>
<li>link(url) <code>&lt;a href=&quot;url&quot;&gt;string&lt;/a&gt;</code></li>
<li>small() <code>&lt;small&gt;string&lt;/small&gt;</code></li>
<li>strike() <code>&lt;strike&gt;string&lt;/strike&gt;</code></li>
<li>sub() <code>&lt;sub&gt;string&lt;/sub&gt;</code></li>
<li>sup() <code>&lt;sup&gt;string&lt;/sup&gt;</code></li>
</ul>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>感谢您的支持！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>打 赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="http://ofy7k3v4s.bkt.clouddn.com/wcpayCode.jpg" alt="Hongqin Ma WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="http://ofy7k3v4s.bkt.clouddn.com/apayCode.jpg" alt="Hongqin Ma Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/js/" rel="tag">#js</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2014/05/11/08-js-的数组/" rel="next" title="js 的数组">
                <i class="fa fa-chevron-left"></i> js 的数组
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2014/05/24/10-js-的函数/" rel="prev" title="js 的函数">
                js 的函数 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <div class="ds-share flat" data-thread-key="2014/05/15/09-js-String对象/"
     data-title="js String对象"
     data-content=""
     data-url="https://hongqinma.github.io/2014/05/15/09-js-String对象/">
  <div class="ds-share-inline">
    <ul  class="ds-share-icons-16">

      <li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
      <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
      <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
      <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
      <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>

    </ul>
    <div class="ds-share-icons-more">
    </div>
  </div>
</div>
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2014/05/15/09-js-String对象/"
           data-title="js String对象" data-url="https://hongqinma.github.io/2014/05/15/09-js-String对象/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            Overview
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/photo.jpg"
               alt="Hongqin Ma" />
          <p class="site-author-name" itemprop="name">Hongqin Ma</p>
          <p class="site-description motion-element" itemprop="description">Spell at all costs， to rush you in the future. </p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">68</span>
              <span class="site-state-item-name">posts</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">16</span>
                <span class="site-state-item-name">categories</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">57</span>
                <span class="site-state-item-name">tags</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/HongqinMa" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/HongqinMa" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.facebook.com/profile.php?id=100006581058735" target="_blank" title="Facebook">
                  
                    <i class="fa fa-fw fa-facebook"></i>
                  
                  Facebook
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://www.zhihu.com/people/HirakuMa" target="_blank" title="知乎">
                  
                    <i class="fa fa-fw fa-share-alt"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/rubylouvre/" title="司徒正美" target="_blank">司徒正美</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/sanshi/" title="三生石上" target="_blank">三生石上</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/TomXu/" title="汤姆大叔" target="_blank">汤姆大叔</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://gold.xitu.io/" title="掘金" target="_blank">掘金</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://segmentfault.com/" title="segmentfault" target="_blank">segmentfault</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.jianshu.com/users/1cbb94e5ed4d/latest_articles" title="简书" target="_blank">简书</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://home.cnblogs.com/" title="博客园" target="_blank">博客园</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://www.awesomes.cn/" title="Awesomes" target="_blank">Awesomes</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#String-类型"><span class="nav-number">1.</span> <span class="nav-text">String 类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#字符方法"><span class="nav-number">2.</span> <span class="nav-text">字符方法:</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#charAt-和-charCodeAt"><span class="nav-number">2.1.</span> <span class="nav-text">charAt() 和 charCodeAt()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#slice-、substring-和-substr"><span class="nav-number">2.2.</span> <span class="nav-text">slice()、substring() 和 substr()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#toLocaleUpperCase-、toUpperCase-、toLocaleLowerCase-、toLowerCase"><span class="nav-number">2.3.</span> <span class="nav-text">toLocaleUpperCase()、toUpperCase()、toLocaleLowerCase()、toLowerCase()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#indexOf-和-lastIndexOf"><span class="nav-number">2.4.</span> <span class="nav-text">indexOf() 和 lastIndexOf()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#字符串匹配模式方法"><span class="nav-number">2.5.</span> <span class="nav-text">字符串匹配模式方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#localCompare"><span class="nav-number">2.6.</span> <span class="nav-text">localCompare()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#fromCharCode"><span class="nav-number">2.7.</span> <span class="nav-text">fromCharCode()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#HTML-方法"><span class="nav-number">2.8.</span> <span class="nav-text">HTML 方法</span></a></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Hongqin Ma</span>
</div>

<div class="powered-by">
  Powered by <a class="theme-link" href="https://hexo.io">Hexo</a>
</div>

<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span id="busuanzi_container_site_pv">
    &nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;Page Views: <span id="busuanzi_value_site_pv"></span> 
</span>

        

<div class="busuanzi-count">

  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv"><i class="fa fa-user"></i><span class="busuanzi-value" id="busuanzi_value_site_uv"></span></span>
  

  
    <span class="site-pv"><i class="fa fa-eye"></i><span class="busuanzi-value" id="busuanzi_value_site_pv"></span></span>
  
  
</div>



        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"Hiraku"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  

  

  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  
  <!-- custom analytics part create by xiamo -->
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
<script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
<script>
function showTime(Counter) {
  var query = new AV.Query(Counter);
  $(".leancloud_visitors").each(function() {
    var url = $(this).attr("id").trim();
    query.equalTo("url", url);
    query.find({
      success: function(results) {
        if (results.length == 0) {
          var content = '0 ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
          return;
        }
        for (var i = 0; i < results.length; i++) {
          var object = results[i];
          var content = object.get('time') + ' ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
        }
      },
      error: function(object, error) {
        console.log("Error: " + error.code + " " + error.message);
      }
    });

  });
}

function addCount(Counter) {
  var Counter = AV.Object.extend("Counter");
  url = $(".leancloud_visitors").attr('id').trim();
  title = $(".leancloud_visitors").attr('data-flag-title').trim();
  var query = new AV.Query(Counter);
  query.equalTo("url", url);
  query.find({
    success: function(results) {
      if (results.length > 0) {
        var counter = results[0];
        counter.fetchWhenSave(true);
        counter.increment("time");
        counter.save(null, {
          success: function(counter) {
            var content =  counter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(counter, error) {
            console.log('Failed to save Visitor num, with error message: ' + error.message);
          }
        });
      } else {
        var newcounter = new Counter();
        newcounter.set("title", title);
        newcounter.set("url", url);
        newcounter.set("time", 1);
        newcounter.save(null, {
          success: function(newcounter) {
              console.log("newcounter.get('time')="+newcounter.get('time'));
            var content = newcounter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(newcounter, error) {
            console.log('Failed to create');
          }
        });
      }
    },
    error: function(error) {
      console.log('Error:' + error.code + " " + error.message);
    }
  });
}
$(function() {
  var Counter = AV.Object.extend("Counter");
  if ($('.leancloud_visitors').length == 1) {
    addCount(Counter);
  } else if ($('.post-title-link').length > 1) {
    showTime(Counter);
  }
}); 
</script>

  
</body>
</html>
